package cn.itsource.pet.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetRedisLoginVoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.baidumap.DistanceUtil;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.pojo.domain.SearchMasterMsg;
import cn.itsource.pet.pojo.dto.AuditDto;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.SearchMasterMsgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg, SearchMasterMsgQuery> implements SearchMasterMsgService {
    @Autowired
   private SearchMasterMsgMapper msgMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Override
    public PageList<SearchMasterMsg> findPageList(SearchMasterMsgQuery query) {
        //设置店铺id条件，别人店铺不应该展示
        Long shopId = GetRedisLoginVoUtil.getShopId();
        query.setShopId(shopId);
        //1.分页插件
        //PageHelper.startPage(query.getPage(),query.getSize());
        //2.查询  这两行不能写代码
        List<SearchMasterMsg> searchMasterMsgPageList = msgMapper.findPageList(query);
        //健壮性判断
        if (CollUtil.isEmpty(searchMasterMsgPageList)){
            return new PageList<>();
        }
        //根据5km的范围进行过滤  配置：在哪配置才能写活，数据库
        Shop shop = shopMapper.selectByPrimaryKey(shopId);

        //准备：1.获取店铺地址  2.发布人宠物地址  3. 5km
        //stream：变成流，filter：以两个地址5km之内过滤条件  .collect(Collectors.toList())：符合过滤条件的数据收集成list
        List<SearchMasterMsg> list = searchMasterMsgPageList.stream().filter(item -> DistanceUtil.isInCircle(shop.getAddress(), item.getAddress(), 5000))
                .collect(Collectors.toList());

        if (CollUtil.isEmpty(list)){
            return new PageList<>();
        }
        //对有效数据手动分页，用lamdba表达式  limit (page-1)*size,size  limit 5 ,5
        List<SearchMasterMsg> resultList = list.stream().skip((query.getPage() - 1) * query.getSize()).limit(query.getSize()).collect(Collectors.toList());

        //4.封装自定义的分页对象
        return new PageList<>((long)list.size(),resultList);
    }
//接单审核
    @Override
    public AjaxResult audit(AuditDto auditDto) {
//        修改审核状态与留言
        msgMapper.audit(auditDto);



     return AjaxResult.createSuccess();
    }
//查询 员工信息
    @Override
    public  List<Employee>  employeeInformation() {
        Long shopId = GetRedisLoginVoUtil.getShopId();
        List<Employee> employees = msgMapper.employeeInformation(shopId);
        return employees;
    }


}
